package logic

import (
	"context"

	"github.com/comfygpt-backend-go/common/consts"
	"github.com/comfygpt-backend-go/common/jwtx"
	"github.com/comfygpt-backend-go/service/user-api/internal/svc"
	"github.com/comfygpt-backend-go/service/user-api/internal/types"

	"github.com/comfygpt-backend-go/service/user-rpc/userclient"

	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserRequest) (resp *types.CommonResp, err error) {
	resp = &types.CommonResp{
		Code:    int64(consts.SUCCESS),
		Message: consts.WrongMessageEn[consts.SUCCESS],
	}
	in := &userclient.UpdateInfoRequest{
		Id:    jwtx.GetUidFromCtx(l.ctx),
		Name:  req.Name,
		Email: req.Email,
	}

	_, err = l.svcCtx.UserRpc.UpdateInfo(l.ctx, in)
	logc.Info(l.ctx, "UpdateInfo err: %v", err)
	if err != nil {
		return nil, err
	}

	return
}
